package com.jsz.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jsz.constant.MessageConstant;
import com.jsz.entity.PageResult;
import com.jsz.entity.QueryPageBean;
import com.jsz.entity.Result;
import com.jsz.pojo.CheckGroup;
import com.jsz.service.CheckGroupService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 检查组管理
 */
@RestController
@RequestMapping("/checkGroup")
public class CheckGroupController {
    @Reference
    private CheckGroupService checkGroupService;

    //添加数据
    @RequestMapping("/add/{checkitemIds}")
    @PreAuthorize("hasAuthority('CHECKGROUP_ADD')")//设置添加数据权限
    public Result add(@RequestBody CheckGroup checkGroup, @PathVariable Integer[] checkitemIds) {
        //调试
        //System.out.println("CheckGroupController中的add方法调用了 checkitemIds:" + checkitemIds + "-----" + "checkGroup:" + checkGroup);
        try {
            //调用业务层操作数据
            checkGroupService.add(checkGroup, checkitemIds);
            //添加数据成功
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //添加数据失败
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    //分页查询
    @RequestMapping("/findByPage")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean) {
        //调试
        //System.out.println("CheckGroupController中的findByPage方法被调用了     queryPageBean = " + queryPageBean);
        //调用业务层操作数据
        PageResult pageResult = checkGroupService.findByPage(queryPageBean);
        return pageResult;
    }

    //根据ID查询数据
    @RequestMapping("/findById/{id}")
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")//设置查询权限
    public Result findById(@PathVariable Integer id) {
        try {
            CheckGroup checkGroup = checkGroupService.findById(id);
            //查询成功,回显成功消息
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    //根据ID查询中间表
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")//设置查询权限
    @RequestMapping("/findCheckItemIdsByCheckGroupId/{id}")
    public Result findCheckItemIdsByCheckGroupId(@PathVariable Integer id) {
        try {
            List<Integer> checkItemIds = checkGroupService.findCheckItemIdsByCheckGroupId(id);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItemIds);
        } catch (Exception e) {
            e.printStackTrace();
            //查询失败
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    //编修修改数据
    @RequestMapping("/update/{checkitemIds}")
    @PreAuthorize("hasAuthority('CHECKGROUP_EDIT')")//设置修改数据权限
    public Result update(@RequestBody CheckGroup checkGroup, @PathVariable Integer[] checkitemIds) {
        try {
            //调用业务层操作数据
            checkGroupService.update(checkGroup, checkitemIds);
            //修改数据成功
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //修改数据失败
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }

    //根据ID删除数据
    @RequestMapping("/deleteByCheckGroupId/{id}")
    @PreAuthorize("hasAuthority('CHECKGROUP_DELETE')")//设置删除数据权限
    public Result deleteByCheckGroupId(@PathVariable Integer id) {
        try {
            //调用业务层操作数据
            checkGroupService.deleteByCheckGroupId(id);
            //删除数据成功
            return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //删除数据失败
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }

    //查询所有检查组信息
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            //调用业务层操作数据
            List<CheckGroup> checkGroup = checkGroupService.findAll();
            //查询成功
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroup);
        } catch (Exception e) {
            e.printStackTrace();
            //查询失败
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
}
